﻿using System;
using System.Data;
using System.Data.OracleClient;

namespace Data.Oracle.Ora
{
    public class DbOraClient : Database
    {
        OracleDataReader rd = null;
        OracleTransaction trn = null;

        public override System.Data.IDbConnection CreateConnection()
        {
            return new OracleConnection();
        }

        public override System.Data.IDbConnection CreateConnection(string cnnstr)
        {
            return new OracleConnection(cnnstr);
        }

        public override System.Data.IDbCommand CreateCommand()
        {
            return new OracleCommand();
        }

        public override System.Data.IDbTransaction CreateTransaction()
        {
            return trn;
        }

        public override System.Data.IDataReader CreateReader()
        {
            return rd;
        }

        public override System.Data.IDbCommand CreateCommand(string commandText, System.Data.IDbConnection connection)
        {
            return new OracleCommand(commandText, (OracleConnection)connection);
        }

        public override System.Data.IDbCommand CreateCommand(string commandText, System.Data.IDbConnection connection, System.Data.IDbTransaction trn)
        {
            return new OracleCommand(commandText, (OracleConnection)connection, (OracleTransaction)trn);
        }

        public override System.Data.IDbDataAdapter CreateAdapter(string commandText, string connectionString)
        {
            return new OracleDataAdapter(commandText, connectionString);
        }

        public override System.Data.IDbDataAdapter CreateAdapter(string commandText, System.Data.IDbConnection connection)
        {
            return new OracleDataAdapter(commandText, (OracleConnection)connection);
        }

        public override System.Data.IDbDataAdapter CreateAdapter(System.Data.IDbCommand cmd)
        {
            return new OracleDataAdapter((OracleCommand)cmd);
        }

        public override System.Data.IDbDataAdapter CreateAdapter()
        {
            return new OracleDataAdapter();
        }

        public override IDbCommand CreateCommand(CommandType commandType, string commandText)
        {
            OracleCommand cmd = new OracleCommand(commandText);
            cmd.CommandType = commandType;
            return cmd;
        }

        public override IDbDataParameter ParamAddWithValue(string paramName, object value)
        {
            return new OracleParameter(paramName, value);
        }

        public override void DeriveCommand(IDbCommand cmd)
        {
            OracleCommandBuilder.DeriveParameters((OracleCommand)cmd);
        }

        public override IDbCommand CreateCommand(string commandText)
        {
            return new OracleCommand(commandText);
        }

        public override string GetParameterToken()
        {
            throw new NotImplementedException();
        }

        public override IDbDataParameter CreateParam<T>(string paramName, DbType tip, T value)
        {
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = paramName;
            prm.DbType = tip;
            prm.Value = value;
            return prm;
        }

        public override string ExecuteProcConvert(string sqlcmd)
        {
            string sqltxt = sqlcmd.Replace("'", "''");
            return "EXECUTE IMMEDIATE '" + sqltxt + "'";
        }
    }
}
